/**
 * @Author: Bobby
 * @Date: 2022-03-21 09:17:11
 * @LastEditTime: 2022-03-21 09:47:40
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\eventTask\actions\form\formItemReset.js
 * @CIMC-V2
 */

import { processingConstant, getDefaultDate, getComponentDefaulType, getModelByModelTree } from '@ve/utils/utils'
// import cloneDeep from 'lodash/cloneDeep'
import isArray from 'lodash/isArray'
import isEmpty from 'lodash/isEmpty'
import trim from 'lodash/trim'
import dayjs from 'dayjs'
//必须带done回调 执行完成调用 vm, action, done
export default function formItemReset({ vm, action, done }) {
  // console.log('重置表单项！！！！', vm, action)
  const notUseDefaultVal = action.extension?.notUseDefaultVal?.value
  isArray(action.extension.formItemSelect.value) &&
    action.extension.formItemSelect.value.forEach((fromItemId) => {
      const _self = vm.vuiComponents[fromItemId]?.self
      if (_self && fromItemId.indexOf('uiDynamicFormLot') > -1) {
        // 如果选择的是批次属性组件 则查找对应的表单组件 然后清空批次属性的值
        const modelTree = vm.$store.state.preview.pages[vm.$store.state.preview.nowPageId]
        let _formModule = getModelByModelTree(_self.formId, modelTree)
        for (let i = 1; i < 49; i++) {
          const indexStr = i < 10 ? `0${i}` : i
          _formModule.data.formData[`LOT${indexStr}`] = ''
        }
      } else {
        if (_self && _self.setFormDataModel) {
          if (notUseDefaultVal) {
            // 如果不使用默认值 则直接清空值为默认类型空值
            _self.setFormDataModel(getComponentDefaulType(_self.getInfo.type))
          } else {
            // 赋默认值
            if (_self.getInfo.extension?.defaultValue?.value) {
              // console.log(`触发寻找默认值~~~~~~~~${_self.getInfo._id} --------------------- ${_self.getInfo.extension.defaultValue.value}`)
              _self.setFormDataModel(processingConstant(vm, _self.getInfo.extension.defaultValue.value))
              //选择新
              if (_self.getInfo.type.indexOf('uiSelectLayerNew') !== -1) {
                _self.setFormDataModel(processingConstant(vm, _self.getInfo.extension.defaultValue.value))
              }
            } else {
              //选择新
              if (_self.getInfo.type.indexOf('uiSelectLayerNew') !== -1) {
                // 查找表单项组件所在的表单组件 然后重置表单组件对应的 model 和 text 的绑定值
                const modelField = _self.getInfo.extension.model.value
                const textField = _self.getInfo.extension.fieldKey.value
                const modelTree = vm.$store.state.preview.pages[vm.$store.state.preview.nowPageId]
                let _formModule = getModelByModelTree(_self.formId, modelTree)
                modelField && _formModule && (_formModule.data.formData[modelField] = '')
                textField && _formModule && (_formModule.data.formData[textField] = '')
                // _self.setFormDataModel('')
              }
              //开关按钮单独获取值
              if (_self.getInfo.type === 'uiSwitch') {
                let switchValue = null
                if (_self.getInfo.extension.defaultState.value) {
                  switchValue = _self.getInfo.extension.activeValue.value
                } else {
                  switchValue = _self.getInfo.extension.inactiveValue.value
                }
                _self.setFormDataModel(switchValue)
              } else if (_self.getInfo.type === 'uiDate') {
                // 日期时间组件单独获取值
                if (_self.getInfo.extension.defaultDate.value) {
                  // 如果设置默认值为 当前
                  if (_self.getInfo.extension.selection.value === 'datetime' || _self.getInfo.extension.selection.value === 'date') {
                    // 如果当前组件类型为 datetime 或 date
                    if (_self.getInfo.extension.defaultDate.value === 'now') {
                      // 设置 默认值为 当前 的默认值
                      let defaultDate = getDefaultDate('now')
                      // 如果为日期类型则去掉值的时分秒
                      if (_self.getInfo.extension.selection.value.indexOf('time') == -1) {
                        _self.setFormDataModel(defaultDate.split(' ')[0])
                      } else {
                        _self.setFormDataModel(defaultDate)
                      }
                    } else {
                      _self.setFormDataModel('')
                    }
                  } else if (_self.getInfo.extension.selection.value === 'datetimerange' || _self.getInfo.extension.selection.value === 'daterange') {
                    // 如果当前组件类型为 时间范围
                    if (_self.getInfo.extension.defaultDate.value !== 'now') {
                      let defaultDate = getDefaultDate(_self.getInfo.extension.defaultDate.value)
                      // 如果为日期类型则去掉值的时分秒
                      if (_self.getInfo.extension.selection.value.indexOf('time') == -1) {
                        defaultDate[0] = defaultDate[0].split(' ')[0]
                        defaultDate[1] = defaultDate[1].split(' ')[0]
                      }
                      _self.setFormDataModel(defaultDate)
                    } else {
                      _self.setFormDataModel('')
                    }
                  }
                } else {
                  // 没有默认值则置为空
                  _self.setFormDataModel('')
                }
              } else if (_self.getInfo.type === 'uiTimePicker') {
                // 时间组件单独获取值
                if (_self.getInfo.extension.defaultTime && _self.getInfo.extension.defaultTime.value) {
                  const timeReg = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/
                  const defaultTimeStr = _self.getInfo.extension.defaultTime.value
                  // 如果是时间段
                  if (_self.getInfo.extension.selection.value) {
                    // const startSuffix = _self.getInfo.extension?.startSuffix?.value ? _self.getInfo.extension?.startSuffix?.value : '_start'
                    // const endSuffix = _self.getInfo.extension?.endSuffix?.value ? _self.getInfo.extension?.endSuffix?.value : '_end'
                    // 判断值格式是否正确
                    if (defaultTimeStr.indexOf(',') === -1) return
                    else {
                      let defaultTimeList = defaultTimeStr.split(',')
                      defaultTimeList[0] = trim(defaultTimeList[0])
                      defaultTimeList[1] = trim(defaultTimeList[1])
                      if (defaultTimeList[0] === '#CURRENT_TIME#' || defaultTimeList[1] === '#CURRENT_TIME#') {
                        defaultTimeList.forEach((item, index) => {
                          if (item === '#CURRENT_TIME#') {
                            defaultTimeList[index] = dayjs().format('HH:mm:ss')
                          }
                        })
                      }
                      if (timeReg.test(defaultTimeList[0]) && timeReg.test(defaultTimeList[1])) {
                        _self.setFormDataModel(defaultTimeList)
                        // defaultValue[_self.getInfo.extension.model.value] = defaultTimeList
                        // defaultValue[`${_self.getInfo.extension.model.value}${startSuffix}`] = defaultTimeList[0] || ''
                        // defaultValue[`${_self.getInfo.extension.model.value}${endSuffix}`] = defaultTimeList[1] || ''
                      } else {
                        _self.setFormDataModel('')
                      }
                    }
                  } else {
                    if (defaultTimeStr === '#CURRENT_TIME#') {
                      // 如果是获取当前时间
                      _self.setFormDataModel(dayjs().format('HH:mm:ss'))
                      // defaultValue[_self.getInfo.extension.model.value] = dayjs().format('HH:mm:ss')
                    } else {
                      // 校验时间格式
                      if (timeReg.test(defaultTimeStr)) {
                        _self.setFormDataModel(defaultTimeStr)
                        // defaultValue[_self.getInfo.extension.model.value] = defaultTimeStr
                      } else {
                        _self.setFormDataModel('')
                      }
                    }
                  }
                }
              } else {
                _self.setFormDataModel(getComponentDefaulType(_self.getInfo.type))
              }
            }
          }

          // 清除_formData 中的老值
          const _formData = vm.vuiComponents[_self.formId]?.self.getInfo?.data?._formData || {}
          if (!isEmpty(_formData)) {
            _formData[_self.getInfo.extension?.model?.value] =
              vm.vuiComponents[_self.formId]?.self.getInfo?.data?.formData[[_self.getInfo.extension?.model?.value]]
          }
          // _formData[_self.getInfo.extension?.model?.value] = getComponentDefaulType(_self.getInfo.type)
          // 触发组件的值改变事件
          _self.onChange && _self.onChange()
        }
      }
    })
  done(null, {})
}
